/**
 * Created by sunyxq on 16/3/22.
 */
angular.module('hyt.shrCtrl',[])

    //客户签收列表
  .controller('ShrListCtrl',function($scope,ShrCydListFactory,$ionicPopup,Storage,$ionicHistory,YqListFactory,$location,$timeout,$stateParams,$ionicTabsDelegate){
      $scope.showloading=true;
      var dw_id=Storage.get("dw_id");
      var sid=Storage.get('sid');
      var user_id=Storage.get('user_id');
      var yh_id=Storage.get('yh_id');
      var zd_id=Storage.get('zd_id');

      ShrCydListFactory.getCydList(dw_id,sid,user_id,yh_id,zd_id);

      YqListFactory.getCydList(dw_id,sid,user_id,yh_id,zd_id);

      $scope.$on('CydList.cydUpdated', function() {
          var dataFactory=ShrCydListFactory.getCyd();
          console.log(dataFactory);
          var data=dataFactory.data;
          $scope.cydData = data;
      });
      $scope.$on('YqList.cydUpdated',function(){
          var yqData=YqListFactory.getCyd();
          console.log(yqData);
          var yqList=yqData.data;
          $scope.yqCyd=yqList;
          $scope.showloading=false;
      });

     //下拉更新
     $scope.doRefresh=function(){
        ShrCydListFactory.getCydList(dw_id,sid,user_id,yh_id,zd_id);
        $scope.$broadcast('scroll.refreshComplete');
     };

    $scope.doRefresh1=function(){
        YqListFactory.getCydList(dw_id,sid,user_id,yh_id,zd_id);
        $scope.$broadcast('scroll.refreshComplete');
    };
    //上拉更新
    $scope.loadMore=function(){
        ShrCydListFactory.getMoreCyd(dw_id,sid,user_id,yh_id,zd_id);
        $scope.$broadcast('scroll.infiniteScrollComplete');
    };

    $scope.loadMore1=function(){
        YqListFactory.getMoreCyd(dw_id,sid,user_id,yh_id);
        $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //下一页
    $scope.hasNextPage = function() {
       return ShrCydListFactory.hasNextPage();
    };

    $scope.hasNextPage1 = function() {
        return YqListFactory.hasNextPage();
    };

    $scope.selectTab=function(){
        Storage.set('tabBs',0);
        ShrCydListFactory.getCydList(dw_id,sid,user_id,yh_id,zd_id);
    };

    $scope.selectTab1=function(){
        Storage.set('tabBs',1);
    };

      //获得送货方式
      $scope.getStatus= function (shfs) {
          switch (shfs) {
              case 1:
                  $scope.shfs = "自提";
                  break;
              case 2 :
                  $scope.shfs = '送货';
                  break;
              case 3 :
                  $scope.shfs = '上楼';
                  break;
              default :
                  $scope.shfs='';
                  break;
          }
          return $scope.shfs;
      };
      if (Storage.get('tabBs')==1){
          $timeout(function(){
              $ionicTabsDelegate.select(1);
          },300);
      }
  })
    //列表查询
  .controller('ShrListSearchCtrl',function($scope,$state,$stateParams){
      $scope.cydcx={
         cydh:'',
         shrmc:$stateParams.khmc,
         shrdh:$stateParams.lxdh,
         shrmcdh:''
      };
      if ($stateParams.khmc!=''||$stateParams.lxdh!=''){
          $scope.cydcx.shrmcdh=$stateParams.khmc+'/'+$stateParams.lxdh;
      }
      $scope.searchCyd=function(cydh,shrmc,shrdh){
          $state.go("shrsearchresult",{cydh:cydh,shrmc:shrmc,shrdh:shrdh});
          console.log(cydh);
          console.log(shrmc);
      };
      //选择联系人
      $scope.chooseLinkman = function (htmlbs,sflx) {
          $state.go("linkman",{htmlbs:htmlbs,sflx:sflx});
      };
  })
    //列表查询结果
    .controller('ShrSearchResultCtrl',function($scope,SearchListFactory,$stateParams,Storage,$ionicHistory){
        var dw_id=Storage.get("dw_id");
        var zd_id=Storage.get("zd_id");
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        $scope.showloading=true;
        $scope.moveItem = function(item, fromIndex, toIndex) {
            $scope.items.splice(fromIndex, 1);
            $scope.items.splice(toIndex, 0, item);
        };
        //下拉更新
        $scope.doRefresh=function(){
            SearchListFactory.queryCyd(dw_id,$stateParams.cydh,$stateParams.shrmc,$stateParams.shrdh,sid,user_id,yh_id,zd_id);
            $scope.$broadcast('scroll.refreshComplete');
        };

        //上拉更新
        $scope.loadMore=function(){
            SearchListFactory.getMoreCyd(dw_id,$stateParams.cydh,$stateParams.shrmc,$stateParams.shrdh,sid,user_id,yh_id,zd_id);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };

        //下一页
        $scope.hasNextPage = function() {
            return SearchListFactory.hasNextPage();
        };
        SearchListFactory.queryCyd(dw_id,$stateParams.cydh,$stateParams.shrmc,$stateParams.shrdh,sid,user_id,yh_id,zd_id);
        $scope.$on('SearchList.cydUpdated',function(){
            var dataFactory=SearchListFactory.getCyd();
            $scope.cydData=dataFactory.data;
            $scope.showloading=false;
            console.log($scope.cydData);
        });
        $scope.myGoBack = function() {
            $ionicHistory.goBack();
        };
    })
    //客户签收--签收详情
    .controller('ShrQsDetailCtrl',function($scope,$stateParams,ShrCydListFactory,Storage,$state,$ionicLoading,$ionicPopup,$timeout){
        $scope.msg={
            shrmc:$stateParams.shrmc,
            shrdh:$stateParams.shrdh,
            qssl:$stateParams.qssl,
            sfzh:'',
            cylx:$stateParams.cylx,
            mdd:$stateParams.mdd
        };
        var cyd_id=$stateParams.cyd_id;
        var dw_id=Storage.get("dw_id");
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var zd_id=Storage.get('zd_id');
        var zdmc=Storage.get('zdmc');
        var bmmc;
        if( $scope.msg.cylx==1){
            bmmc=zdmc;
        }else {
            bmmc=$scope.msg.mdd;
        }
        //客户签收
       $scope.confirmQs=function(){
           if($scope.msg.shrmc==''&&$scope.msg.shrdh==''&&$scope.msg.sfzh==''){
               $ionicLoading.show({
                   noBackdrop: true,
                   template: "请输入签收人名称、电话或身份证号！",
                   duration: 1500
               });
           }else if (!($scope.msg.shrdh.length==11&&$scope.msg.shrdh&& /^1[3|4|5|7|8]\d{9}$/.test($scope.msg.shrdh))){
               $ionicLoading.show({
                   noBackdrop: true,
                   template: "请输入合法手机号！",
                   duration: 1500
               });
           }else {
               var confirmPopup = $ionicPopup.confirm({
                   title:'签收',
                   template: '确定签收吗？',
                   okText:'确定',
                   cancelText:'取消'
               });
               confirmPopup.then(function(res) {
                   if (res) {
                       ShrCydListFactory.qianshou(dw_id,sid,user_id,yh_id,cyd_id,zd_id,$scope.msg.shrmc,$scope.msg.shrdh,$scope.msg.sfzh,$scope.msg.qssl,bmmc);
                       $scope.$on('QsUpdated',function(){
                           var result=ShrCydListFactory.getQsResult();
                           if(result=='000000'){
                               $ionicLoading.show({
                                   noBackdrop: true,
                                   template: "签收成功！",
                                   duration: 600
                               });
                           }else {
                               $ionicLoading.show({
                                   noBackdrop: true,
                                   template: "签收失败！",
                                   duration: 600
                               });
                           }
                           $timeout(function(){
                               $state.go('shrlist');
                           },160);

                       });
                   } else {

                   }
               });
           }
       };
    })
    //客户拒收--拒收详情
    .controller('ShrJsDetailCtrl',function($scope,$stateParams,$filter,Storage,$ionicPopup,ShrCydListFactory,$ionicLoading,$state,$timeout){
        $scope.msg={
            hs:'',
            hc:'',
            bz:'',
            hsun:''
        };

        var haTime = new Date();
        $scope.fsrq=$filter('date')(haTime,'yyyy-MM-dd');
        $scope.showEndDate = function(){
            var happenTime = {
                date : haTime,
                mode : 'date'
            };
            datePicker.show(happenTime,function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        haTime=d;
                        $scope.fsrq = $filter('date')(haTime,'yyyy-MM-dd');
                    });
                }
            });
        };

        var cyd_id=$stateParams.cyd_id;
        var cylx=$stateParams.cylx;
        var zdmc=Storage.get("zdmc");
        var bmmc;
        var mdd=$stateParams.mdd;
        if(cylx==1){
            bmmc=zdmc;
        }else {
            bmmc=mdd;
        }
        var dw_id=Storage.get("dw_id");
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var zd_id=Storage.get('zd_id');
        var zsxm=Storage.get('zsxm');
        console.log(cyd_id);
        //货损登记展开列表
        $scope.dengji=function(){
            if($("#hsdj").hasClass("hide")){
                $("#hsdj").removeClass("hide").addClass("show");
            }else {
                $("#hsdj").removeClass("show").addClass("hide");
                //收起列表时清空列表内容
                $("#hs").val("");
                $("#hc").val("");
                $("#hd").val("");
            }
        }

        console.log($scope.fsrq);

        //点击确定拒收
        $scope.confirmJs=function(){
            if($scope.msg.bz==''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入拒收理由！",
                    duration: 1500
                });
            }else {
                var confirmPopup = $ionicPopup.confirm({
                    title:'拒收',
                    template: '确定拒收吗？',
                    okText:'确定',
                    cancelText:'取消'
                });
                confirmPopup.then(function(res) {
                    if (res) {
                        ShrCydListFactory.jushou(dw_id,sid,user_id,yh_id,cyd_id,$scope.msg,zsxm,$scope.fsrq,zd_id,bmmc);
                        $scope.$on('JsUpdated',function(){
                            var result=ShrCydListFactory.getJsResult();
                            if(result.ACTION_RETURN_CODE=='000000'){
                                $ionicLoading.show({
                                    noBackdrop: true,
                                    template: "拒收成功！",
                                    duration: 1500
                                });
                                $timeout(function(){
                                    $state.go("shrlist",{},{reload:true});
                                },160);
                            }else {
                                $ionicLoading.show({
                                    noBackdrop: true,
                                    template: "拒收失败！",
                                    duration: 1500
                                });
                                $state.go('shrlist');
                            }
                        })

                    } else {

                    }
                });
            }
        };


    })